<?php

namespace app\index\controller;

use app\index\logic\Oss;

class UploadImg extends Common
{

    protected $user_id;

    public function _initialize()
    {
        parent::_initialize();
        //$this->user_id = $this->_checkPassport($this->_getParams('passport'));
    }

    /**
     * 图片上传 \n
     * URI : /upload/img
     * @param :
     *  name  |  type  | null | description
     * -------|--------|------|-------------
     *  sign  | string | 必填 | 签名
     *
     * @return
     *    name      |  type   | description
     * -------------|---------|----------------------
     *   img_list   |  array  |  列表
     *
     */
    public function uploadImg()
    {
        $img_list = $this->saveImg();
        if ($img_list)
            $this->_returnData(['img_list' => $img_list]);
        $this->_returnError(20001, '上传失败');
    }

    public function saveImg()
    {
        $oss = new Oss();
        $res = false;
        $img_path = [];
        foreach ($_FILES as $key => $value) {
            if (is_array($value['tmp_name'])) {
                foreach ($value['tmp_name'] as $k => $val) {
                    $res = \think\Db::name('img')->order('id DESC')->find();
                    $img_new_path = ($res['id'] + 1) . "." . substr($value['type'][$k], 6);
                    $res = $oss->uploadFile($img_new_path, $value['tmp_name'][$k]);
                    $img_path[] = OSS_SITE_URL . "/" . $img_new_path;
                    \think\Db::name('img')->insert(['img_url' => OSS_SITE_URL . "/" . $img_new_path]);
                }
            } elseif (is_string($value['tmp_name'])) {
                $res = \think\Db::name('img')->order('id DESC')->find();
                $img_new_path = ($res['id'] + 1) . "." . substr($value['type'], 6);
                $res = $oss->uploadFile($img_new_path, $value['tmp_name']);
                $img_path[] = OSS_SITE_URL . "/" . $img_new_path;
                \think\Db::name('img')->insert(['img_url' => OSS_SITE_URL . "/" . $img_new_path]);
            }
        }
        if ($res)
            return $img_path;
        return false;
    }


    /**
     * 上传信息 \n
     * URI : /upload/info
     * @param :
     *  name  |  type  | null | description
     * -------|--------|------|-------------
     *  sign  | string | 必填 | 签名
     *  title | string | 必填 | 标题
     *  cat   | string | 必填 | 分类（以','分隔开）
     *  img_url| string | 必填 | 图片地址（以json字符串传值）
     *
     * @return
     *    name      |  type   | description
     * -------------|---------|----------------------
     *   --------   |  -----  |  -------
     *
     */
    public function updateInfo()
    {
        $title = $this->_getParams('title');
        $cat = $this->_getParams('cat');
        $img_url = json_decode($this->_getParams('img_url'), true);
        foreach($img_url as $value) {
            \think\Db::name('info')->insert([
                'img_url' => $value,
                'title' => $title,
                'cat' => $cat,
            ]);
        }
        $this->_returnData();
    }
}
